import { Controller } from 'egg';

export default class extends Controller {
  // 上传图片
  async uploadImage() {
    const { ctx, service } = this;
    const stream = await ctx.getFileStream();
    const imgTypes = ['jpg', 'png', 'gif', 'jpeg'];
    const filename = stream.filename;
    const nameArray = filename.split('.');
    const type = nameArray[nameArray.length - 1];
    if (imgTypes.indexOf(type) === -1) {
      ctx.fail('请上传图片格式的文件', 40005)
      return;
    }
    const result = await service.base.upload(stream);
    ctx.ok(result, '上传成功');
  }
  //上传文件
  async uploadFile() {
    const { ctx, service } = this;
    const stream = await ctx.getFileStream();
    const result = await service.base.upload(stream);
    ctx.ok(result, '上传成功');
  }
}
